﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Main_Inventory_StockJournalDisplay : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindJounalFormat();
            if (Request.QueryString["ReturnCategory"] != null)
            {
                ddlstkjrnCategory.SelectedValue = Request.QueryString["ReturnCategory"].ToString();
                ddlstkjrnCategory_OnSelectedIndex(this, EventArgs.Empty);
            }
        }
        
    }
    protected void BindJounalFormat()
    {
        DataTable dtformate = DU.getdatausingprocedure("usp_ivr_DisplayGetTransactionFormat"); // Onliy for Stock Journal Type
        if (dtformate.Rows.Count > 0)
        {
            ddlstkjrnCategory.DataSource = dtformate;
            ddlstkjrnCategory.DataTextField = "FORMATNAME";
            ddlstkjrnCategory.DataValueField = "TFOID";  //Not  STKJRNLOPT
            ddlstkjrnCategory.DataBind();

            ListItem li = new ListItem("--- Select Format ---", "0");
            ddlstkjrnCategory.Items.Insert(0, li);
            li.Selected = true;
        }
    }
    protected void ddlstkjrnCategory_OnSelectedIndex(object sender, EventArgs e)
    {
        if (ddlstkjrnCategory.SelectedValue != "0")
        {
            //SqlParameter[] parma = { 
            //                          new SqlParameter("@TFOID",ddlstkjrnCategory.SelectedValue)
            //                       };
            //DataTable DTstkjrn = DU.getdatausingprocedure("USP_IVR_StockJournal_Display", parma);
            //hid_TFOID.Value = "";
            //string strsql = "Select STKJRNLOPT from IVR_TRANFORMAT Where TFOID='" + ddlstkjrnCategory.SelectedValue + "'";
            //DataTable DT = DU.getdata(strsql);
            //if (DT.Rows.Count > 0)
            //{
            //    if (DT.Rows[0]["STKJRNLOPT"].ToString() == "1") // Stock Journal
            //    {
            //        hid_TFOID.Value = "1";
            //    }
            //    else if (DT.Rows[0]["STKJRNLOPT"].ToString() == "2")  // Production Journal
            //    {
            //        hid_TFOID.Value = "2";
            //    }
            //    else if (DT.Rows[0]["STKJRNLOPT"].ToString() == "3")  // Stock Issue
            //    {
            //        hid_TFOID.Value = "3";
            //    }
            //    else if (DT.Rows[0]["STKJRNLOPT"].ToString() == "4")  // Stock Receipt
            //    {
            //        hid_TFOID.Value = "4";
            //    }
            //    else if (DT.Rows[0]["STKJRNLOPT"].ToString() == "5")  // Stock Transfer
            //    {
            //        hid_TFOID.Value = "5";
            //    }
            //    else if (DT.Rows[0]["STKJRNLOPT"].ToString() == "6")  // Physical Stock
            //    {
            //        hid_TFOID.Value = "6";
            //    }
            //}

            DataTable DTstkjrn = DU.getdata("select * from IVR_STKJRNL inner join ivr_tranformat on IVR_STKJRNL.tfoid= ivr_tranformat.tfoid where ivr_tranformat.stkjrnlopt=" + ddlstkjrnCategory.SelectedValue + "");
            if (DTstkjrn.Rows.Count > 0)
            {
                grdstkjrnldisp.DataSource = DTstkjrn;
                grdstkjrnldisp.DataBind();
            }

          
        }
    }
    protected void grdstkjrnldisp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string TFOID = grdstkjrnldisp.DataKeys[e.Row.RowIndex].Values[0].ToString();
            string SJRID = grdstkjrnldisp.DataKeys[e.Row.RowIndex].Values[1].ToString();
           
            string TFOID_Row = ((Label)e.Row.FindControl("lblformatname")).Text.ToString();

            DataTable dtformat = DU.getdata("select formatname from ivr_tranformat where tfoid=" + TFOID_Row + "");
            if (dtformat.Rows.Count > 0)
            {
                //Label lblformat = (Label)grdstkjrnldisp.Rows[e.Row.RowIndex].Cells[0].Controls[1];
                //lblMsg.Text += lblformat.Text.ToString();
                ((Label)e.Row.FindControl("lblformatname")).Text = dtformat.Rows[0]["FORMATNAME"].ToString();
            }

            //lblMsg.Text += "select (case   when  numprefix != '' then numprefix+'/' else numprefix end) +convert(varchar,ivr_stkjrnl.sjrnum)+(case   when  numsuffix != '' then '/'+ numsuffix else numsuffix end)as Numberformat   from IVR_TRANFORMATNUM inner join ivr_stkjrnl on ivr_stkjrnl.tfoid=IVR_TRANFORMATNUM.tfoid where ivr_stkjrnl.sjrid=" + SJRID + "<br/>";

            DataTable dtNumber = DU.getdata("select (case when numprefix != '' then numprefix+'/' else numprefix end) +convert(varchar,ivr_stkjrnl.sjrnum)+(case when numsuffix != '' then '/'+ numsuffix else numsuffix end)as Numberformat from IVR_TRANFORMATNUM inner join ivr_stkjrnl on ivr_stkjrnl.tfoid=IVR_TRANFORMATNUM.tfoid where ivr_stkjrnl.sjrid=" + SJRID + "");
            if (dtNumber.Rows.Count > 0)
            {
                ((Label)e.Row.FindControl("lblformatno")).Text = dtNumber.Rows[0]["Numberformat"].ToString();
            }
            string FormatName = ((Label)e.Row.FindControl("lblformatname")).Text;
            string FormatNo = ((Label)e.Row.FindControl("lblformatno")).Text;
            string Date = ((Label)e.Row.FindControl("lbldate")).Text;
            ((HyperLink)e.Row.FindControl("hypdetails")).Attributes.Add("onclick", "OpenStockDisplay('" + TFOID + "','" + SJRID + "','"+ddlstkjrnCategory.SelectedValue+"','"+FormatName+"','"+FormatNo+"','"+Date+"')");

           //((HyperLink)e.Row.FindControl("hypedit")).Attributes.Add("");
           //((HyperLink)e.Row.FindControl("hypedit")).Attributes.Add("onclick", "javascript:alert('" + hid_TFOID.Value.ToString() + "');");

            ((HyperLink)e.Row.FindControl("hypedit")).NavigateUrl = "StockJournalEdit.aspx?TFOID=" + TFOID + "&SJRID=" + SJRID + "&Category=" + ddlstkjrnCategory.SelectedValue + "&FormatName=" + FormatName + "&FormatNo=" + FormatNo + "&Date=" + Date+"";
        }
    }
}
